package com.soikc.kuaidiE.wx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.soikc.kuaidiE.bean.Courier;
import com.soikc.kuaidiE.bean.KuaiDi;
import com.soikc.kuaidiE.bean.Message;
import com.soikc.kuaidiE.bean.UserEax;
import com.soikc.kuaidiE.mvc.ResponseBody;
import com.soikc.kuaidiE.mvc.ResponseView;
import com.soikc.kuaidiE.service.KuaiDiService;
import com.soikc.kuaidiE.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

public class ExpressController {
    private UserUtil userUtil = new UserUtil();
    private KuaiDiService kuaiDiService = new KuaiDiService();
    @ResponseBody("/api/wx/findExpressList")
    public String findByPhone(HttpServletRequest req, HttpServletResponse res){
        UserEax wxUser = userUtil.getWxUser(req.getSession());
        List<JSONObject> list = new ArrayList<>();
        Message message;
        if (wxUser ==null){
            message = new Message(-1,"获取失败,没有用户信息");
        }else {
            List<KuaiDi> list1 = kuaiDiService.queryPhone(wxUser.getPhone());
            list1.forEach(kuaiDi -> list.add(kuaiDi.toViveObj()));
            message = new Message(0,"获取成功",list);
        }
        return message.toString();
    }
    @ResponseBody("/api/wx/findExpressByCode")
    public String findCode(HttpServletRequest req, HttpServletResponse res){
        String code = req.getParameter("code");
        KuaiDi kuaiDi = kuaiDiService.queryPickup_code(code);
        Message message;
        if (kuaiDi == null){
            message = new Message(-1,"没有这个快递");
        }else {
            message = new Message(0,"查询成功",kuaiDi.toViveObj());
        }
        return message.toString();
    }
    @ResponseBody("/api/wx/updateStatus")
    public String updateStatus(HttpServletRequest req, HttpServletResponse res){
        String id = req.getParameter("id");
        Message message = new Message(-1,"收货失败");
        try {
            boolean b = kuaiDiService.updateStatus(Integer.parseInt(id), 1);
            if (b){
                return new Message(0,"收货成功").toString();
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return message.toString();
    }
    @ResponseBody("/api/wx/addExpress")
    public String addExpress(HttpServletRequest req, HttpServletResponse res) {
        HttpSession session = req.getSession();
        if ((userUtil.getWxUser(session) instanceof Courier)==false){
            return new Message(-1,"你没有权限录入快递").toString();
        }
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        KuaiDi k = new KuaiDi();
        k.setSingle_number(number);
        k.setCompany(company);
        k.setUser_name(username);
        k.setPhone(userPhone);
        k.setDepositor_phone(userUtil.getUserPhone(session));
        boolean add = kuaiDiService.add(k);
        Message message = new Message(add ? 0 : -1,add?"入录成功":"录入失败");
        return JSON.toJSONString(message);
    }

    @ResponseView("/api/wx/createQRCode")
    public String createQRCode(HttpServletRequest req, HttpServletResponse res){
        String code = req.getParameter("code");
        String type = req.getParameter("type");
        HttpSession session = req.getSession();
        if ("express".equals(type)){
            session.setAttribute("qrcode","express_"+code);
        }else if ("user".equals(type)){
            UserEax wxUser = userUtil.getWxUser(session);
            session.setAttribute("qrcode","user_"+(wxUser==null?"":wxUser.getPhone()));
        }
        return "/personQRcode.html";
    }
    @ResponseBody("/api/wx/getQRCode")
    public String getQRCode(HttpServletRequest req, HttpServletResponse res){
        HttpSession session = req.getSession();
        String qrcode = (String) session.getAttribute("qrcode");
        Message message;
        if (qrcode == null){
            message = new Message(-1,"获取失败","获取失败");
        }else {
            message = new Message(0,qrcode,qrcode);
        }
        return message.toString();
    }
    @ResponseView("/api/wx/findExpress")
    public String findExpress(HttpServletRequest req, HttpServletResponse res){
        String expressNum = req.getParameter("expressNum");
        String url = "https://kuaidi100.com/result.jsp?nu="+expressNum;
        return url;
    }
}
